'use strict';
function submitF1() {
    if ($("#UserName").val() == "" || $("#UserName").val() == "用户名") {
        showError('请输入用户名', '#UserName');
        return false;
    }
    if ($("#Password").val() == "" || $("#Password").val() == "密码") {
        showError('请输入密码<br>六位以上字符', '#Password');
        return false;
    }
    /*$.ajax({
        type: "POST",
        data: $('#loginform1').serialize(),
        url:  readRealUrl("api-account-login"),
        dataType:"json",
        beforeSend: function(){  $("#submitbtn1").loading(1); },
        success: function (result) {
            window.Token = result.data.Token;
            setTimeout(function(){ $("#submitbtn1").loading(); },1e3);
            if (result.code != "200") {
                showError(result.msg, '#submitbtn1');
            }*/
            var result = {};
            result.data = {};
            result.data.res_code = Math.ceil( Math.random()*3 );
            result.data.accountSecretKey = "123123";
            result.data.res = "服务器爆炸了";
            //////////////////////////////////////
            if (result.data.res_code == "1") {
                window.accountSecretKey = result.data.accountSecretKey;
                $("#loginbox").fadeOut(function(){ $("#pinbox").fadeIn() });
                keydownEnt = submitF2;
                return;
            }else if (result.data.res_code == "3") {
                showBindBox();
                return;
            }else if (result.data.res_code == "2") {
                showError("账号或密码不正确", '#submitbtn1');
                $("#Password").val("");//取消密码
            }else {
                showError(result.data.res, '#submitbtn1');
            }
        /*}
    });*/
}
function submitF2() {
    var vv = $("#pin2").val();
    //判断非空
    if (vv == "" || vv == "Google PIN码") {
        showError('请输入Pin码', '#pin2');
        return false;
    }
    //判断是否为六位数字
    if(!/^[0-9]{6}$/.test(vv)){
        showError('请输入6位数字', '#pin2');
        return false;
    }
    /*$.ajax({
        type: "POST",
        data: { "isbinding":1,"c":vv,"PaccountSecretKey":window.accountSecretKey,Token:window.Token },
        url:  readRealUrl("api-account-GoogleAuthenticator"),
        dataType:"json",
        beforeSend: function(){  $("#submitbtn1").loading(1); },
        success: function (result) {
            setTimeout(function(){ $("#submitbtn1").loading(); },1e3);
            if (result.code != "200") {
                showError(result.msg, '#submitbtn2');
                return;
            }*/
            var result = {};
            result.data = {};
            result.data.res_code = Math.ceil( Math.random()*2 ) - 1;
            result.data.rolename = "";
            result.data.res = "";
            //////////////////////////////////////
            if (result.data.res_code==0) {
                showError(result.data.res||"绑定谷歌Pin码失败", '#pin2');
                return;
            }
            if (result.data.rolename.indexOf("商务")!=-1) {
                window.location.href = "index.html#/ui/busi_main";
                return;
            }
            window.location.href = "index.html#!/app/produce/table10";
        /*}
    });*/
}
function showBindBox(){
    $.ajax({
        type: "POST",
        url:  readRealUrl("api-account-GoogleAuthenticator"),
        data: { "isbinding":0,"c":null,"PaccountSecretKey":null,Token:window.Token },
        dataType:"json",
        beforeSend: function(){  $("#submitbtn1").loading(1); /*不可点击*/ },
        success: function (result) {
            setTimeout(function(){ $("#submitbtn1").loading(); /*可以点击*/ },1e3);
            //"res_code":"1", //正确1 错误2
            //"res":"ok"//错误返回错误码res_code、错误信息res
            if (result.code != "200") {
                showError(result.msg, '#submitbtn1');
                return;
            }
            window.accountSecretKey = result.data.AccountSecretKey||"";
            window.manualEntryKey = result.data.ManualEntryKey||"";
            var qrcodetext = "otpauth://totp/wangwei:"+$('#UserName').val()+"?secret="+window.manualEntryKey;
            $("#weima2").qrcode({
                width: 150, height:150, text: qrcodetext,
                typeNumber:-1, correctLevel:2,
                background:"#ffffff", foreground:"#000000" });
            $("#loginbox").fadeOut(function(){ $("#bindbox").fadeIn() });
            keydownEnt = submitF3;
        }
    });
}
function submitF3() {
    var vv = $("#pin3").val();
    if (vv == "" || !/^[0-9]{6}$/.test(vv) || vv == "Google PIN码") {
        showError('请输入Pin码', '#pin3');
        return false;
    }
    $.ajax({
        type: "POST",
        data: { "isbinding":1,"c":vv,"PaccountSecretKey":window.accountSecretKey,Token:window.Token },
        url:  readRealUrl("api-account-GoogleAuthenticator"),
        dataType:"json",
        success: function (result) {
            if (result.code != "200") {
                showError(result.msg, '#submitbtn3');
                return;
            }
            if (result.data.res==0) {
                showError("绑定谷歌Pin码失败", '#submitbtn3');
                return;
            }
            saveFun();
        }
    });
}
function saveFun() {
    $.ajax({
        type: "get",
        data: {
          accountSecretKey:window.accountSecretKey,
          Token:window.Token
        },
        url:  readRealUrl("api-account-bindask"),
        dataType:"json",
        success: function (result) {
            if (result.code != "200") {
                showError(result.msg, '#submitbtn3');
                return;
            }
            if (result.data.res_code==0) {
                showError("绑定谷歌Pin码失败", '#submitbtn3');
                return;
            }
            window.location.href = "index.html";
        }
    });
}
function readRealUrl(url){
    return "/"+url.replace(/\-/g,"/");
}
function showError(msg, id) {
    $(id).parent().addClass("has-error-x");
    $(id).one("focus", function () { $(this).parent().removeClass("has-error-x") });
    var ep = ($(document.body).width() > 480) ? 2 : 1;//判断错误提示的位置
    layer.tips(msg, id, { tips: [ep, "#F74D4D"], tipsMore: true });
}
var keydownEnt = submitF1;
$("body").keydown(function () {if (event.keyCode == "13") { keydownEnt() ; return false} });//回车提交事件//keyCode=13是回车键
exports = module.exports = function() {
    (function($){
        $.fn.loading = function(state,name){
            if(state){ name = (typeof(name)=="undefined")?"loading":name; $(this).attr("disabled",true).attr("loading-text",$(this).html()).html(name); }
            else{ $(this).attr("disabled",false).html($(this).attr("loading-text")); }
        }
    })(jQuery)
    $("#submitbtn1").click(submitF1);
    $("#submitbtn2").click(submitF2);
    $("#submitbtn3").click(submitF3);
}